package com.jyw.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.jyw.common.constant.UserConstants;
import com.jyw.common.utils.DateUtils;
import com.jyw.system.domain.SysConfig;
import com.jyw.system.domain.SysOrderReviewDetailConfig;
import com.jyw.system.domain.dto.SysOrderReviewConfigAddDto;
import com.jyw.system.domain.dto.SysOrderReviewDetailConfigAddDto;
import com.jyw.system.domain.vo.SysOrderReviewConfigAllVo;
import com.jyw.system.domain.vo.SysOrderReviewConfigVo;
import com.jyw.system.domain.vo.SysOrderReviewDetailConfigVo;
import com.jyw.system.enums.OrderReviewDetailEnum;
import com.jyw.system.enums.OtherConfigEnum;
import com.jyw.system.service.ISysConfigService;
import com.jyw.system.service.ISysOrderReviewConfigService;
import com.jyw.system.service.ISysOrderReviewDetailConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author Created by ZhangCheng on 2023/08/17 (系统工单审核配置)
 */
@Service
public class SysOrderReviewConfigServiceImpl implements ISysOrderReviewConfigService {

    @Autowired
    private ISysConfigService sysConfigService;

    @Autowired
    private ISysOrderReviewDetailConfigService sysOrderReviewDetailConfigService;

    @Override
    public SysOrderReviewConfigAllVo getConfig() {
        SysOrderReviewConfigAllVo sysOrderReviewConfigAllVo = new SysOrderReviewConfigAllVo();
        SysConfig sysConfig = sysConfigService.findConfigByKey(OtherConfigEnum.ORDER_REVIEW.getKey());
        if (sysConfig != null && StrUtil.isNotEmpty(sysConfig.getConfigValue())) {
            SysOrderReviewConfigVo reviewConfig = JSONObject.parseObject(sysConfig.getConfigValue(),
                    SysOrderReviewConfigVo.class);
            sysOrderReviewConfigAllVo.setSysOrderReviewConfig(reviewConfig);
        }
        List<SysOrderReviewDetailConfigVo> faultSourceList = sysOrderReviewDetailConfigService.queryFaultSourceList();
        if (CollectionUtil.isNotEmpty(faultSourceList)) {
            sysOrderReviewConfigAllVo.setFaultSourceList(faultSourceList);
        }
        List<SysOrderReviewDetailConfigVo> faultTypeList = sysOrderReviewDetailConfigService.queryFaultTypeList();
        if (CollectionUtil.isNotEmpty(faultTypeList)) {
            sysOrderReviewConfigAllVo.setFaultTypeList(faultTypeList);
        }
        List<SysOrderReviewDetailConfigVo> staffList = sysOrderReviewDetailConfigService.queryStaffList();
        if (CollectionUtil.isNotEmpty(staffList)) {
            sysOrderReviewConfigAllVo.setStaffList(staffList);
        }
        return sysOrderReviewConfigAllVo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(SysOrderReviewConfigAddDto sysOrderReviewConfigAddDto) {
        if (sysOrderReviewConfigAddDto.getSysOrderReviewConfig() != null) {
            SysOrderReviewConfigVo configVo = sysOrderReviewConfigAddDto.getSysOrderReviewConfig();
            updateConfig(configVo);
        }
        //来源
        Integer faultSource = OrderReviewDetailEnum.FAULT_SOURCE.getValue();
        sysOrderReviewDetailConfigService.deleteByType(faultSource);
        if (CollectionUtil.isNotEmpty(sysOrderReviewConfigAddDto.getFaultSourceList())) {
            List<SysOrderReviewDetailConfigAddDto> faultSourceList = sysOrderReviewConfigAddDto.getFaultSourceList();
            List<SysOrderReviewDetailConfig> configList = getConfigList(faultSourceList,
                    faultSource);
            sysOrderReviewDetailConfigService.saveBatch(configList);
        }
        //分类
        Integer faultType = OrderReviewDetailEnum.FAULT_TYPE.getValue();
        sysOrderReviewDetailConfigService.deleteByType(faultType);
        if (CollectionUtil.isNotEmpty(sysOrderReviewConfigAddDto.getFaultTypeList())) {
            List<SysOrderReviewDetailConfigAddDto> faultTypeList = sysOrderReviewConfigAddDto.getFaultTypeList();
            List<SysOrderReviewDetailConfig> configList = getConfigList(faultTypeList,
                    faultType);
            sysOrderReviewDetailConfigService.saveBatch(configList);
        }
        //人员
        Integer staff = OrderReviewDetailEnum.STAFF.getValue();
        sysOrderReviewDetailConfigService.deleteByType(staff);
        if (CollectionUtil.isNotEmpty(sysOrderReviewConfigAddDto.getStaffList())) {
            List<SysOrderReviewDetailConfigAddDto> staffList = sysOrderReviewConfigAddDto.getStaffList();
            List<SysOrderReviewDetailConfig> configList = getConfigList(staffList,
                    staff);
            sysOrderReviewDetailConfigService.saveBatch(configList);
        }
    }

    /**
     * 获取明细
     *
     * @param list
     * @param type
     * @return
     */
    public List<SysOrderReviewDetailConfig> getConfigList(List<SysOrderReviewDetailConfigAddDto> list, Integer type) {
        List<SysOrderReviewDetailConfig> configList = new ArrayList<>();
        AtomicInteger index = new AtomicInteger(0);
        list.forEach(a -> {
            int newIndex = index.get() + 1;
            index.set(newIndex);
            SysOrderReviewDetailConfig config = new SysOrderReviewDetailConfig();
            config.setType(type);
            config.setRecordId(a.getRecordId());
            config.setSort(newIndex);
            config.setCreateTime(DateUtils.getNowDate());
            configList.add(config);
        });
        return configList;
    }

    public void updateConfig(SysOrderReviewConfigVo sysOrderReviewConfigVo) {
        SysConfig sysConfig = sysConfigService.findConfigByKey(OtherConfigEnum.ORDER_REVIEW.getKey());
        String configValue = JSONObject.toJSONString(sysOrderReviewConfigVo);
        if (sysConfig != null) {
            sysConfig.setConfigValue(configValue);
            sysConfig.setUpdateTime(DateUtils.getNowDate());
            sysConfigService.updateConfig(sysConfig);
        } else {
            initConfig(configValue);
        }
    }

    /**
     * 初始化配置数据
     *
     * @param configValue
     */
    public void initConfig(String configValue) {
        SysConfig sysConfig = new SysConfig();
        sysConfig.setConfigKey(OtherConfigEnum.ORDER_REVIEW.getKey());
        sysConfig.setConfigName(OtherConfigEnum.ORDER_REVIEW.getName());
        sysConfig.setConfigValue(configValue);
        sysConfig.setConfigType(UserConstants.YES);
        sysConfig.setCreateTime(DateUtils.getNowDate());
        sysConfigService.insertConfig(sysConfig);
    }
}
